package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysCustomer;
import com.ruoyi.system.mapper.SysCustomerMapper;
import com.ruoyi.system.mapper.SysUserCustomerMapper;
import com.ruoyi.system.service.ISysCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysCustomerServiceImpl implements ISysCustomerService
{
    @Autowired
    private SysCustomerMapper customerMapper;

    @Autowired
    private SysUserCustomerMapper userCustomerMapper;

    /**
     * 查询客户信息集合
     * 
     * @param customer 客户信息
     * @return 客户信息集合
     */
    @Override
    public List<SysCustomer> selectCustomerList(SysCustomer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 查询所有客户
     * 
     * @return 客户列表
     */
    @Override
    public List<SysCustomer> selectCustomerAll()
    {
        return customerMapper.selectCustomerAll();
    }

    /**
     * 根据用户ID查询客户
     * 
     * @param userId 用户ID
     * @return 客户列表
     */
    @Override
    public List<SysCustomer> selectCustomersByUserId(Long userId)
    {
        List<SysCustomer> userCustomers = customerMapper.selectCustomersByUserId(userId);
        List<SysCustomer> customers = customerMapper.selectCustomerAll();
        return customers;
    }

    /**
     * 通过客户ID查询客户信息
     * 
     * @param customerId 客户ID
     * @return 角色对象信息
     */
    @Override
    public SysCustomer selectCustomerById(Long customerId)
    {
        return customerMapper.selectCustomerById(customerId);
    }

    /**
     * 批量删除客户信息
     * 
     * @param ids 需要删除的数据ID
     * @throws Exception
     */
    @Override
    public int deleteCustomerByIds(String ids) throws BusinessException
    {
        Long[] customerIds = Convert.toLongArray(ids);
        for (Long customerId : customerIds)
        {
            SysCustomer customer = selectCustomerById(customerId);
            if (countUserCustomerById(customerId) > 0)
            {
                throw new BusinessException(String.format("%1$s已分配,不能删除", customer.getCustomerName()));
            }
        }
        return customerMapper.deleteCustomerByIds(customerIds);
    }

    /**
     * 新增保存客户信息
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int insertCustomer(SysCustomer customer)
    {
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改保存客户信息
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int updateCustomer(SysCustomer customer)
    {
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 通过客户ID查询客户使用数量
     * 
     * @param customerId 客户ID
     * @return 结果
     */
    @Override
    public int countUserCustomerById(Long customerId)
    {
        return userCustomerMapper.countUserCustomerById(customerId);
    }

    /**
     * 校验客户名称是否唯一
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public String checkCustomerNameUnique(SysCustomer customer)
    {
        Long customerId = StringUtils.isNull(customer.getCustomerId()) ? -1L : customer.getCustomerId();
        SysCustomer info = customerMapper.checkCustomerNameUnique(customer.getCustomerName());
        if (StringUtils.isNotNull(info) && info.getCustomerId().longValue() != customerId.longValue())
        {
            return UserConstants.CUSTOMER_NAME_NOT_UNIQUE;
        }
        return UserConstants.CUSTOMER_NAME_UNIQUE;
    }

    /**
     * 校验客户电话是否唯一
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public String checkCustomerPhoneUnique(SysCustomer customer)
    {
        Long customerId = StringUtils.isNull(customer.getCustomerId()) ? -1L : customer.getCustomerId();
        SysCustomer info = customerMapper.checkCustomerPhoneUnique(customer.getCustomerPhone());
        if (StringUtils.isNotNull(info) && info.getCustomerId().longValue() != customerId.longValue())
        {
            return UserConstants.CUSTOMER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.CUSTOMER_PHONE_UNIQUE;
    }
}
